package com.lhnstart.learn.bases.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 定制sort排序
 *
 * @author lhn
 * @date 2023/08/03 21:15
 * @since 1.0.0
 **/
public class ArraysSortCustom {

    public static void main(String[] args) {
        int[] arr = {1, -1, 7, 0, 89};
        //bubble01(arr);
        bubble02(arr, (o1, o2) -> o2 - o1);
        System.out.println(Arrays.toString(arr));
    }

    public static void bubble01(int[] arr) {
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    // 结合冒泡加定制
    public static void bubble02(int[] arr, Comparator<Integer> comparator) {
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i -1; j++) {
                if (comparator.compare(arr[j], arr[j + 1]) > 0) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

}